<?php
declare (strict_types = 1);

namespace app\index\controller;




use app\BaseController;
use app\index\model\GoodsProp;
use think\facade\Cache;
use think\facade\Db;
use think\facade\Request;
use think\facade\View;

class Goodspropvalue extends BaseController
{

    //
    public function goods_prop()
    {


        $prop_value = Request::get('prop_value/a', [], 'trim,htmlspecialchars,strip_tags');
        $pname = Request::get('pname', '', 'trim,htmlspecialchars,strip_tags');
        $get = Request::get();
        //$data = Request::get('','','trim,htmlspecialchars,strip_tags');
        $data = [
            'prop_value' => $prop_value,
            'pname' => $pname,
        ];
        if (Cache::has('arr')) {
            $arr = Cache::get('arr');
        } else {
            $arr = \app\index\model\GoodsPropValue::getPropValue();
            Cache::set('arr', $arr, 600);
        }
        View::assign('arr', $arr);

        $sx = \app\index\model\GoodsProp::getGoodsProp($data, $data, []);
        $list = \app\index\model\GoodsProp::getGoodsProp($data, $get);

        View::assign('data', $data);
        View::assign('list', $list);
        View::assign($get);


        $empty = "<tr><td colspan='7'>暂无数据！</td></tr>";
        View::assign('empty', $empty);
        return View::fetch('goodspropvalue/goods_prop', ['sx' => $sx, 'arr' => $arr]);
    }

    public function add_goods_prop()
    {


        if (Request::isPost()) {

            $post = Request::post('', '', 'trim,htmlspecialchars,strip_tags');

//            $post['pvalues'] = implode(',', $post['pvalues']);
            try {

                validate(\app\validate\Goodsprop::class)->check($post);
            } catch (\think\exception\ValidateException $e) {
                // 验证失败 输出错误信息
                 $this->error($e->getMessage());
            }


            $jumpPath = '';
            if ('1' == $post['button']) $jumpPath = url('goods_prop');
            if ('2' == $post['button']) $jumpPath = url('add_goods_prop');

            $propObj = new \app\index\model\GoodsProp();
            $result = $propObj->add($post);

            if ($result) {
                $this->success("成功！" . $propObj->getInsertId(), $jumpPath);

            } else {
                $this->error("失败！原因：" . $propObj->getError());
            }
        } else {
            if (Cache::has('arr')) {
                $arr = Cache::get('arr');
            } else {
                $arr = \app\index\model\GoodsPropValue::getPropValue();
                Cache::set('arr', $arr, 600);
            }
            View::assign('arr', $arr);
            return View::fetch();
        }
    }

    public function edit_goods_prop()
    {


        if (Cache::has('arr')) {
            $arr = Cache::get('arr');
        } else {
            $arr = \app\index\model\GoodsPropValue::getPropValue();
            Cache::set('arr', $arr, 600);
        }
        View::assign('arr', $arr);


        $param = Request::param('param', [], 'trim,htmlspecialchars,strip_tags');
        if (Request::isPost()) {
            $post = Request::post('', '', 'trim,htmlspecialchars,strip_tags');

            try {

                validate(\app\validate\Goodsprop::class)->scene('Edit')->check($post);
            } catch (\think\exception\ValidateException $e) {
                // 验证失败 输出错误信息
                $this->error($e->getMessage());
            }


            $propObj = new \app\index\model\GoodsProp();
            $result = $propObj->edit($post);

            if ($result) {
                unset($post['button']);
                $this->success('修改成功！', url('goods_prop', $param));
            } else {
                $this->error('修改失败！');
            }


        } else {
            // 显示修改的页面
            $id = Request::get('id', '', 'trim,htmlspecialchars,strip_tags');

            $propObj = \app\index\model\GoodsProp::find($id)->getData();

            $propObj['pvalues'] = explode(',', $propObj['pvalues']);
            if (empty($propObj)) {
                $this->error('参数错误！', url('goods_prop'));
            }

            return View::fetch('', $propObj);
        }

    }

    //删除
    public function del()
    {
        $id = Request::get('id', '', '');


        Db::startTrans();

        try {
            \app\index\model\GoodsProp::destroy($id, true);

            //提交事务
            Db::commit();

        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('删除成功!',getenv("HTTP_REFERER"));


    }

    //多删
    public function all_del(){
       $id =Request::get('id',"",'');


       $id = explode(',',$id);
        Db::startTrans();

            try{
                \app\index\model\GoodsProp::destroy($id);

                //提交事务
                Db::commit();

            }catch (\Exception $e){
                $this->error($e->getMessage());
                // 回滚事务
                Db::rollback();
            }
        $this->success('删除成功!',getenv("HTTP_REFERER"));

    }
}


